#include "GameServer.h"

#include "_Global/Platform/Platform.h"

#include <QApplication>


void customReleaseMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static QMutex mutex;
    QString text;

    switch(type)
    {
    case QtDebugMsg:
        //return;
        text = QString("Debug:");
        break;

    case QtWarningMsg:
        //return;
        text = QString("Warning:");
        break;

    case QtCriticalMsg:
        text = QString("Critical:");
        break;

    case QtFatalMsg:
        text = QString("Fatal:");
        break;

    default:
        text = QString("Unkown:");
        break;
    }


    QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
    QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    QString current_date = QString("(%1)").arg(current_date_time);
    QString message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_date);


    mutex.lock();

    QFile file(QString(FIXLOCALPATH_W("log_%1.txt")).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd")));
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream text_stream(&file);
    text_stream << message << "\r\n";
    file.flush();
    file.close();

    mutex.unlock();
}

int main(int argc, char *argv[])
{
#ifdef QT_NO_DEBUG
    //在程序开始前添加
    qInstallMessageHandler(customReleaseMessageHandler);
#endif

    QApplication a(argc, argv);
    GameServer w;
    w.show();

    return a.exec();
}
